package com.xiaoq.endpoint;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.xiaoq.coms.GenericDataResponse;
import com.xiaoq.coms.GenericResponse;
import com.xiaoq.vo.QAResponseVo;

import bitoflife.chatterbean.AliceBot;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import bitoflife.chatterbean.Context;
import bitoflife.chatterbean.Graphmaster;
import bitoflife.chatterbean.aiml.Category;
import bitoflife.chatterbean.text.Request;
import bitoflife.chatterbean.text.Response;
import bitoflife.chatterbean.text.Sentence;
import bitoflife.chatterbean.text.Transformations;

@Api(tags={"智能问答自己业务部分"})
@RestController
@RequestMapping("/api/v1/qa")
public class QAEndpoint {
	
	@Autowired
	private AliceBot bot;
	
	private class QADataResponse extends GenericDataResponse<QAResponseVo> {}
	
	private static final Logger LOG = LoggerFactory.getLogger(QAEndpoint.class);
	
	@ApiOperation(value = "智能问答接口", notes = "智能问答接口")
	@ApiResponses({ @ApiResponse(code = 200, response = QADataResponse.class, message = "智能问答接口") })
	@GetMapping(value = "/asking", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> ask(
			@RequestParam(value = "askSecense", required = true) @ApiParam(value = "问句", required = true) String askSecense) {

		QAResponseVo qaResponse = new QAResponseVo();
		//sentence = xx_xx 前面表示查询目标,后面表示查询条件
		String sentence = bot.respond(askSecense);
		
		String[] resultList = sentence.trim().split("_");
		if(resultList.length>1){
			qaResponse.setSentence(resultList[1]);
			qaResponse.setTagetType(resultList[0]);
		}else{
			qaResponse.setSentence(resultList[0]);
		}
		return QADataResponse.okWithData(qaResponse);

	}
	
	public void learn(String pattern, String template)
    {
     
 
      Context context = bot.getContext();
      context.getRequests(1);  // 这个有可能就是输入值第几次输入
      Transformations transformations = context.getTransformations();

      pattern = transformations.normalization(pattern);

      Category category = new Category(pattern, new String[] {template});
      Graphmaster brain = bot.getGraphmaster();
      brain.append(category);
    }
	
	@ApiOperation(value = "获取之前的记录", notes = "获取之前的记录")
	@ApiResponses({ @ApiResponse(code = 200, response = GenericResponse.class, message = "获取之前的记录") })
	@GetMapping(value = "/result", produces = { MediaType.APPLICATION_JSON_UTF8_VALUE })
	public ResponseEntity<?> getHistory(int index){
		 Context context = bot.getContext();
		 Request requst = context.getRequests(index);  // 获取的是倒数第几次请求
		 Sentence sentence = requst.getSentences(0);
		 
//		 String result = sentence.normalized(0); // 这个是一句话做分词之后的词列表
		 return GenericDataResponse.okWithData(sentence.getNormalized());
	}
	
	/**
	 * 查询倒数第index次的提问
	 * @param index 0表示上一句
	 * @return
	 */
	private String getHistoryRequestByIndex(int index){
		 Context context = bot.getContext();
		 Request requst = context.getRequests(index);  // 获取的是倒数第几次请求
		 Sentence sentence = requst.getSentences(0);
		 
//		 String result = sentence.normalized(0); // 这个是一句话做分词之后的词列表
		 return sentence.getNormalized();
	}
	
	/**
	 * 查询倒数第index次的请求
	 * @param index
	 * @return
	 */
	private String getHistoryResponseByIndex(int index){
		 Context context = bot.getContext();
		 Response response = context.getResponses(index);
		 Sentence sentence = response.getSentences(0);
		 return sentence.getNormalized();
	}

}
